<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/10/11
 * Time: 15:34
 */

namespace app\admin\controller;


use Library\Object_Id;
use think\Controller;
use OSS\OssClient;
use OSS\Core\OssException;

class Files extends Controller
{
    public function upload(){
        $path = $this->request->param('path');
        if(empty($path)){
            $path = 'nopath';
        }
        $file = $this->request->file('file');
        $result = $this->validate(['file' => $file], ['file'=>'require|image'],['file.require' => '请选择上传文件', 'file.image' => '非法图像文件']);
        if(true !== $result){
            //$this->error($result);
            return json(array('code'=>99,'error'=>$result));
        }
        if (empty($file)) {
            return json(array('code'=>99,'error'=>'请选择上传文件'));
        }
        // 移动到框架应用根目录/public/uploads/ 目录下
        $object = new Object_Id();
        $key = 'upload/'.$path.'/'.date('Y/m');
        $info = $file->move(ROOT_PATH . 'public' . DS . $key,$object->get_new_id());

        $this->uploadFile($key . '/' .$info->getSaveName());

        if ($info) {
            //$this->success('文件上传成功：' . $info->getRealPath());
            return json(array('code'=>0,'url'=> 'http://pic1.picyq.com/'. $key . '/' .$info->getSaveName()));
        } else {
            // 上传失败获取错误信息
            return json(array('code'=>99,'error'=>$file->getError()));
        }
    }

    public function upload_file(){
        $path = $this->request->param('path');
        if(empty($path)){
            $path = 'nopath';
        }
        $file = $this->request->file('file');
        $result = $this->validate(['file' => $file], ['file'=>'require'],['file.require' => '请选择上传文件']);
        if(true !== $result){
            //$this->error($result);
            return json(array('code'=>99,'error'=>$result));
        }
        if (empty($file)) {
            return json(array('code'=>99,'error'=>'请选择上传文件'));
        }
        // 移动到框架应用根目录/public/uploads/ 目录下
        $object = new Object_Id();
        $key = 'upload/'.$path.'/'.date('Y/m');

        $this->uploadFile($key . '/' .$info->getSaveName());

        $info = $file->move(ROOT_PATH . 'public' . DS . $key,$object->get_new_id());
        if ($info) {
            //$this->success('文件上传成功：' . $info->getRealPath());
            return json(array('code'=>0,'url'=> 'http://pic1.picyq.com/'. $key . '/' .$info->getSaveName()));
        } else {
            // 上传失败获取错误信息
            return json(array('code'=>99,'error'=>$file->getError()));
        }
    }

    // oss 文件上传
    public function uploadFile($data)
    {
        $object   = $data; // 文件保存到 oss 上， 可对文件重命名
        $filePath = $data; // 获取本地文件

        $config=config('aliyun_oss');
        try {
            $ossClient = new \OSS\OssClient($config['KeyId'],$config['KeySecret'],$config['Endpoint'],false);
        } catch (OssException $e) {
            print $e->getMessage();
        }

        try{
            $ossClient->uploadFile($config['Bucket'], $object, $filePath);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }

       //print(__FUNCTION__ . ": OK" . "\n");
     }
}